package edu.gettysburg.catapp1;

import java.io.File;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.support.v4.app.NavUtils;

/**
 * DeleteTour is the activity that gathers the downloaded tours, and displays a list to delete from
 * String[] availTours: private String array to hold list of tour files in directory
 * String DELETE_CHOICE: public String that indicates selected tour, file of title is found and erased
 */
public class deletetour extends Activity {

	private String[] availTours;
	public static String DELETE_CHOICE = "";

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * Builds layout and list based on information from file directories
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.deletetour_layout);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		File tourHolder = getBaseContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

		availTours = tourHolder.list();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menulist, availTours);
		ListView listView = (ListView) findViewById(R.id.deletelistView);
		listView.setAdapter(adapter);

		OnItemClickListener oicl = new OnItemClickListener(){

			/*
			 * (non-Javadoc)
			 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
			 * Has all items in the list act the same way: passes tour name to tour intro class to build from the correct file set
			 */
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				switch (position){  
				default:
					DELETE_CHOICE = availTours[position];
					delete(DELETE_CHOICE);
					back();
					break;
				}
			}
		};
		listView.setOnItemClickListener(oicl);
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * Creates standard options menu- one icon returns home by default
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.intro_layout, menu);
		return true;
	}


	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * Returns to Home.java when home icon is pressed on action bar
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * Accessible back option
	 */
	private void back() {
		super.onBackPressed();
	}

	/**
	 * Deletes a file in the structure based on the given path name
	 * 
	 * @param s: path of file you want to delete
	 * @return boolean: success of the call to delete file
	 * 					(true) if successful
	 * 					(false) otherwise
	 */
	private void delete(final String s) {
		Thread t = new Thread() {
			public void run() {
				String tourPath = null;

				File tourHolder = getBaseContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

				for(int i = 0; i < tourHolder.listFiles().length; i++) {
					if(tourHolder.listFiles()[i].getName().equals(s)) {
						tourPath = tourHolder.listFiles()[i].getPath();
					}
				}

				File deleter = new File(tourPath);
			}
		};
		t.start();
	}

}
